/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.room;

import extension.game.room.handler.ShopHandler;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.Room;
import log.Debug;
import com.smartfoxserver.v2.extensions.SFSExtension;
import extension.game.database.Database;
import extension.game.room.handler.BetMoneyHandler;
import extension.game.room.handler.OnUserGoneHandler;
import extension.game.room.handler.OnUserJoinRoom;
import extension.game.room.handler.PlayerCountHandler;
import extension.game.room.handler.ReadyHandler;
import extension.game.room.handler.RoomCommand;
import extension.game.shop.Shop;
import extension.game.room.handler.LogErrorHandler;

/**
 *
 * @author daz 
 */
public abstract class RoomExtension extends SFSExtension {

    protected Database database;
    public int prefixLength = 1;
    private String roomPrefix = "P";
    private Shop shop;

    public Shop getShop() {
        if (shop == null) {
            shop = new Shop(getParentZone());
        }
        return shop;
    }

    @Override
    public abstract void init();

    public void startupRoom() {
        Debug.setExt(this);
        Room room = getParentRoom();
        if (room.getName().equals("The Lobby")) {
        } else {
            database = new Database(getParentZone().getDBManager());
            roomPrefix = room.getName().substring(0, prefixLength);
            int roomId = Integer.parseInt(room.getName().substring(prefixLength));
            initGame(roomId);
            addHandler();
        }
    }

    public void addHandler() {
        addRequestHandler(RoomCommand.LOG_ERROR, LogErrorHandler.class);
        addRequestHandler(RoomCommand.SHOP, ShopHandler.class);
        addRequestHandler(RoomCommand.PLAYER_READY, ReadyHandler.class);
        addRequestHandler(RoomCommand.BET_MONEY, BetMoneyHandler.class);
        addRequestHandler(RoomCommand.PLAYER_COUNT, PlayerCountHandler.class);
        addEventHandler(SFSEventType.USER_JOIN_ROOM, OnUserJoinRoom.class);
        addEventHandler(SFSEventType.USER_DISCONNECT, OnUserGoneHandler.class);
        addEventHandler(SFSEventType.USER_LEAVE_ROOM, OnUserGoneHandler.class);
    }

    public abstract void initGame(int roomId);

    /* GETTERS & SETTERS */
    public abstract Game getGame();

    public Database getDatabase() {
        return database;
    }

    public String getRoomPrefix() {
        return roomPrefix;
    }

    public abstract GameResponse getResponse();
}
